package com.itdianzi.controller.export;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.ContractExample;
import com.itdianzi.domain.export.Export;
import com.itdianzi.domain.export.ExportExample;
import com.itdianzi.domain.export.ExportProduct;
import com.itdianzi.domain.export.ExportProductExample;
import com.itdianzi.domain.vo.ExportProductVo;
import com.itdianzi.domain.vo.ExportResult;
import com.itdianzi.domain.vo.ExportVo;
import com.itdianzi.service.cargo.ContractService;
import com.itdianzi.service.cargo.ExportProductService;
import com.itdianzi.service.cargo.ExportService;
import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {
    @Reference
    private ExportService exportService;
    @Reference
    private ContractService contractService;
    @Reference
    private ExportProductService exportProductService;

    /**
     *  查询出口报运的数据
     * @param page
     * @param size
     * @return
     */

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page , @RequestParam(defaultValue = "10") Integer size) {

        //需要根据企业ID分类
        ExportExample example = new ExportExample();
       /* //根据日期排序   order by 字段 asc/desc
        example.setOrderByClause("create_Time desc");*/
        //封装条件
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(super.companyId);
        PageInfo pageInfo = exportService.findAll(example, page, size);

        request.setAttribute("page",pageInfo);

        return "cargo/export/export-list";

    }

    /**
     * 查询已经上报的购销合同数据  state=1
     *
     * @param //page
     * @param //size
     * @return
     */
    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {

        //查询合同为1的数据，并且根据用户的企业查询
        ContractExample contractExample = new ContractExample();
        ContractExample.Criteria criteria = contractExample.createCriteria();
        criteria.andStateEqualTo(1);
        criteria.andCompanyIdEqualTo(super.companyId);
        PageInfo pageInfo = contractService.findAll(contractExample, page, size);

        request.setAttribute("page",pageInfo);

        return "cargo/export/export-contractList";

    }

    @RequestMapping("/toExport")
    public String toExport(String id) {
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    @RequestMapping("/edit")
    public String edit(Export export) {

        export.setCompanyId(super.companyId);
        export.setCompanyName(super.companyName);

        if (StringUtils.isBlank(export.getId())) {
            exportService.save(export);

        } else {
            exportService.update(export);

        }
        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/toView")
    public String toView(String id) {

        Export export = exportService.findById(id);

        request.setAttribute("export",export);

        return "cargo/export/export-view";

    }
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //查询报运单对象
        Export export = exportService.findById(id);
        request.setAttribute("export", export);

        //查看报运单货物对象
        ExportProductExample exportProductExample = new ExportProductExample();
        ExportProductExample.Criteria exportProductExampleCriteria = exportProductExample.createCriteria();
        exportProductExampleCriteria.andExportIdEqualTo(id);
        List<ExportProduct> exportProductList = exportProductService.findAll(exportProductExample);
        request.setAttribute("eps",exportProductList);

        return "cargo/export/export-update";

    }
    @RequestMapping("/submit")
    public String submit(String id) {
        Export export = new Export();
        export.setState(1);
        export.setId(id);

        exportService.update(export);

        return "redirect:/cargo/export/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id) {

        exportService.delete(id);

        return "redirect:/cargo/export/list.do";
    }


    /**
     * 出口报运: 电子报运
     * 将我们的数据转换成海关需要的数据 并且传输
     * 1.根据id查询 报运单对象
     * 2.根据id查询到 报运单下货物对象
     * 3.将我们的报运单对象转换成海关的vo对象
     * 4.将我们的报运单下货物对象 转换成海关的vo对象
     * 5.将转换完以后的数据(对象) 传输给海关系统
     * 6.再次调用海关的查询接口 查询刚才的操作是否成功
     * 7.需要算海关的税务金额 , 修改报运单状态
     *
     * @return
     */
     @RequestMapping("/exportE")
    public String exportE(String id) {

         //根据id查询，报运单对象
         Export export = exportService.findById(id);
         //根据id查询到，报运单下货物对象
         ExportProductExample exportProductExample = new ExportProductExample();
         ExportProductExample.Criteria criteria = exportProductExample.createCriteria();
         criteria.andExportIdEqualTo(id);
         //当前报运单下所有的货物数据
         List<ExportProduct> exportProductList = exportProductService.findAll(exportProductExample);
         //将我们的报运单对象转化成海关的vo对象
         ExportVo exportVo = new ExportVo();
         BeanUtils.copyProperties(export, exportVo);
         //赋值exportId
         exportVo.setExportId(id);


         ArrayList<ExportProductVo> list = new ArrayList<>();
         //将我们的报运单下货物对象 转换成海关的vo对象
         //得到每一个报运单下货物对象
         for (ExportProduct exportProduct : exportProductList) {
             //海关对象
             ExportProductVo exportProductVo = new ExportProductVo();
             BeanUtils.copyProperties(exportProduct, exportProductVo);
             //赋值报运货物Id
             exportProductVo.setExportProductId(exportProduct.getId());
             //赋值报运id
             exportProductVo.setExportId(id);
             //加入list中
             list.add(exportProductVo);
         }
         //将list数据交给vo对象
         exportVo.setProducts(list);


         //将转换完的数据（对象）传输给海关系统
         WebClient webClient = WebClient.create("http://localhost:9096/ws/export/user");
         //使用post方式请求给海关
         webClient.post(exportVo);

         //再次调用海关的查询接口，查询刚才的操作是否成功
          webClient = WebClient.create("http://localhost:9096/ws/export/user/" + id);
         ExportResult exportResult = webClient.get(ExportResult.class);
         System.out.println(exportResult);

         //需要算海关的税务金额（货物表中），修改报运单状态
         exportService.updateExport(exportResult);

         return "redirect:/cargo/export/list.do";
    }


}
